package test_page;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;

public class Utils {

    private static WebDriver driver;
    public Utils() {

    }
    public static WebDriver getDriver() {
        if(driver == null) {
            WebDriverManager.firefoxdriver().setup();
            FirefoxOptions options = new FirefoxOptions();
            options.addArguments("--headless");
            driver = new FirefoxDriver();
           driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        }
        return driver;
    }
    public static void openUrl(String url) {
        getDriver().get(url);
    }
    public static void quitDriver() {
        if (driver != null) {
            driver.quit();
            driver = null;
        }
    }

    public static void waitForElementVisible(By locator, Duration timeout) {
        new WebDriverWait(getDriver(), timeout)
                .until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

    public static void waitForElementClickable(By locator, Duration timeout) {
        new WebDriverWait(getDriver(), timeout)
                .until(ExpectedConditions.elementToBeClickable(locator));
    }


    public static void ScreenShot(String name)  {
        //年月日
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        //时分秒
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");

        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());
        File srcFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        String fileName = "./src/test/java/images/" + dirTime + "/" + name +"/"+ "-" + fileTime + ".png";
        try {
            FileUtils.copyFile(srcFile,new File(fileName));
        } catch (IOException e) {
            System.out.println("截图失败:"+e.getMessage());
        }
    }
    public static  WebDriverWait Wait() {
        return new WebDriverWait(getDriver(), Duration.ofSeconds(10));
    }
   public static void captureFullScreen(String picName) {
        //年月日
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        //时分秒
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");

        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());
        File srcFile= ((TakesScreenshot)getDriver()).getScreenshotAs(OutputType.FILE);

        String fileName = "./src/test/java/images/" + dirTime + "/" +picName+"/"+ "-" + fileTime ;
        try {

            // 创建 Robot 类实例
            Robot robot = new Robot();
            // 获取屏幕尺寸
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            // 捕获整个屏幕
            BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
            // 将截图保存为文件
            ImageIO.write(screenFullImage, "png", new File(fileName));
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }

}
